﻿using NDF.Collections;
using NDF.Collections.Generic;
using NDF.Framework.Web.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace NDF.Framework.Web
{
    public class NDFHttpApplication : HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            // 在此方法体中添加控制权限或服务器全局校验的逻辑处理
            // .aspx
            // .ashx
            var ignoresUrl = SystemEnvironment.AuthorizeSettings.IgnoresUrl;   // 表示需要排除验证的路径列表
            var ignoresExt = SystemEnvironment.AuthorizeSettings.IgnoresExt;   // 表示需要排除验证的扩展名列表
            var urlExt = this.Request.GetExtension();
            if (string.IsNullOrWhiteSpace(urlExt)       // 请求的 URL 文件扩展名不能为空
                || ignoresExt.Contains(urlExt)          // 排除指定的 URL 文件扩展名
                || ignoresUrl.Contains(                 // 排除指定的 URL 路径列表
                this.Request.Path, GenericEqualityComparer<string>.Create((a, b) =>
            {
                a = VirtualPathUtility.IsAppRelative(a) ? a : VirtualPathUtility.ToAppRelative(a);
                b = VirtualPathUtility.IsAppRelative(b) ? b : VirtualPathUtility.ToAppRelative(b);
                return a.Equals(b, StringComparison.CurrentCultureIgnoreCase);
            })))
            {
                return;
            }
            // 以下为验证路径访问权限的代码


            //this.Response.End();
        }




        protected void Session_OnStart(object sender, EventArgs e)
        {
            //Console.WriteLine("object: {0}, EventArgs: {1}", sender, e);
        }

        protected void Session_OnEnd(object sender, EventArgs e)
        {
            //Console.WriteLine("object: {0}, EventArgs: {1}", sender, e);
        }
    }
}
